/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.reports.beans;

import edu.soc.cs5226.gui.configuration.beans.Configuration;
import edu.soc.cs5226.gui.configuration.beans.ConfigurationDAO;
import edu.soc.cs5226.gui.entities.util.Constants;
import edu.soc.cs5226.gui.entities.util.TimeUtil;
import edu.soc.cs5226.gui.reports.beans.ReportsBean;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author Huimin
 */
public class GenerateReports {
    
    private ReportsDAO openReportsDAO;
    private static ConfigurationDAO configDAO;
    private static Configuration settingsConfig;
    private static Timestamp startDate;
    private static Timestamp endDate;
    
    public static void main(String[] args) {
        
        int opt = Integer.valueOf(args[0]);
        startDate = new Timestamp(Long.parseLong(args[1]));
        endDate = new Timestamp(Long.parseLong(args[2]));
        
        configDAO = new ConfigurationDAO();
        GenerateReports gr = new GenerateReports();    
        
        try
        {
            settingsConfig = configDAO.getConfiguration();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        switch (opt){
            case 1:
                gr.displaySharedPoolReport();
                break;
            
            case 2:
                gr.displayBufferCacheReport();
                break;    
            
            case 3:
                gr.displayLogBufferReport();
                break;
            
            case 4:
                gr.displaySortMemoryReport();
                break;
            
            case 5:
                gr.displayRollbackSegmentReport();
                break;
                
            case 0:
                System.out.println("Invalid option");
                break;
        }
        
    }
    
    public void displaySharedPoolReport() {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmm");
        
        String reportName = "SharedPoolReport";
        String outputreportName = "SharedPoolReport " + sdf.format(TimeUtil.getInstance().getTime());
        
        openReportsDAO = new ReportsDAO();
        
        try {
                Map parameters = new HashMap();
                parameters.put("startDate", startDate);
                parameters.put("endDate", endDate);
                parameters.put("spsRed", settingsConfig.getRs_redThreshold());
                parameters.put("spsYellow", settingsConfig.getSps_yellowThreshold());
                                                
                JasperFillManager.fillReportToFile(Constants.REPORT_PATH + reportName +
                        ".jasper", parameters, new JRBeanCollectionDataSource(openReportsDAO.generateSPSCollection(startDate, endDate)));

                // create a PDF file
                JasperExportManager.exportReportToPdfFile(Constants.REPORT_PATH + reportName
                        + ".jrprint", Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
                //Desktop Display PDF
                //Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
        } catch (JRException e) {
                e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
        }
    }
    
    public void displayBufferCacheReport() {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmm");
        
        String reportName = "BufferCacheReport";
        String outputreportName = "BufferCacheReport " + sdf.format(TimeUtil.getInstance().getTime());
        
        openReportsDAO = new ReportsDAO();
        
        try {
                Map parameters = new HashMap();
                parameters.put("startDate", startDate);
                parameters.put("endDate", endDate);
                parameters.put("bcRed", settingsConfig.getBc_redThreshold());
                parameters.put("bcYellow", settingsConfig.getBc_yellowThreshold());
                
                JasperFillManager.fillReportToFile(Constants.REPORT_PATH + reportName +
                        ".jasper", parameters, new JRBeanCollectionDataSource(openReportsDAO.generateBCCollection(startDate, endDate)));

                // create a PDF file
                JasperExportManager.exportReportToPdfFile(Constants.REPORT_PATH + reportName
                        + ".jrprint", Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
                //Desktop Display PDF
                //Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
        } catch (JRException e) {
                e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
        }
   }  
   
   public void displayLogBufferReport() {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmm");
        
        String reportName = "RedoLogBufferReport";
        String outputreportName = "RedoLogBufferReport " + sdf.format(TimeUtil.getInstance().getTime());
        
        openReportsDAO = new ReportsDAO();
        
        try {
                Map parameters = new HashMap();
                parameters.put("startDate", startDate);
                parameters.put("endDate", endDate);
                parameters.put("lbRed", settingsConfig.getLb_redThreshold());
                parameters.put("lbYellow", settingsConfig.getLb_yellowThreshold());
                                                
                JasperFillManager.fillReportToFile(Constants.REPORT_PATH + reportName +
                        ".jasper", parameters, new JRBeanCollectionDataSource(openReportsDAO.generateLBCollection(startDate, endDate)));

                // create a PDF file
                JasperExportManager.exportReportToPdfFile(Constants.REPORT_PATH + reportName
                        + ".jrprint", Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
                //Desktop Display PDF
                //Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
        } catch (JRException e) {
                e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
        }
   } 
   
   public void displaySortMemoryReport() {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmm");
        
        String reportName = "SortingMemoryReport";
        String outputreportName = "SortingMemoryReport " + sdf.format(TimeUtil.getInstance().getTime());
        
        openReportsDAO = new ReportsDAO();
        
        try {
                Map parameters = new HashMap();
                parameters.put("startDate", startDate);
                parameters.put("endDate", endDate);
                parameters.put("sRed", settingsConfig.getS_redThreshold());
                parameters.put("sYellow", settingsConfig.getS_yellowThreshold());
                
                JasperFillManager.fillReportToFile(Constants.REPORT_PATH + reportName +
                        ".jasper", parameters, new JRBeanCollectionDataSource(openReportsDAO.generateSCollection(startDate, endDate)));

                // create a PDF file
                JasperExportManager.exportReportToPdfFile(Constants.REPORT_PATH + reportName
                        + ".jrprint", Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
                //Desktop Display PDF
                //Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
        } catch (JRException e) {
                e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
        }
   }
 
   public void displayRollbackSegmentReport() {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmm");
        
        String reportName = "RollbackSegmentReport";
        String outputreportName = "RollbackSegmentReport " + sdf.format(TimeUtil.getInstance().getTime());
        
        openReportsDAO = new ReportsDAO();
        
        try {
                Map parameters = new HashMap();
                parameters.put("startDate", startDate);
                parameters.put("endDate", endDate);
                parameters.put("rsRed", settingsConfig.getRs_redThreshold());
                parameters.put("rsYellow", settingsConfig.getRs_yellowThreshold());
                                                
                JasperFillManager.fillReportToFile(Constants.REPORT_PATH + reportName +
                        ".jasper", parameters, new JRBeanCollectionDataSource(openReportsDAO.generateRSCollection(startDate, endDate)));

                // create a PDF file
                JasperExportManager.exportReportToPdfFile(Constants.REPORT_PATH + reportName
                        + ".jrprint", Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
                //Desktop Display PDF
                //Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + Constants.OUTPUTREPORT_PATH + outputreportName + ".pdf");
                
        } catch (JRException e) {
                e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
        }
   }
   
}
